<template>
  <Transition name="back-to-top">
    <button
      v-show="isVisible"
      @click="scrollToTop"
      class="fixed bottom-6 right-6 w-12 h-12 rounded-full bg-primary text-white shadow-lg flex items-center justify-center transition-all duration-300 hover:bg-accent hover:scale-110 z-50"
      aria-label="回到顶部"
      title="回到顶部"
    >
      <i class="fas fa-chevron-up text-lg"></i>
    </button>
  </Transition>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'

// 响应式数据
const isVisible = ref(false)

// 滚动处理函数
const handleScroll = () => {
  // 当页面滚动超过300px时显示按钮
  isVisible.value = window.pageYOffset > 300
}

// 回到顶部函数
const scrollToTop = () => {
  window.scrollTo({
    top: 0,
    behavior: 'smooth'
  })
}

// 生命周期钩子
onMounted(() => {
  window.addEventListener('scroll', handleScroll)
})

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
})
</script>

<style scoped>
/* 过渡动画 */
.back-to-top-enter-active,
.back-to-top-leave-active {
  transition: all 0.3s ease;
}

.back-to-top-enter-from,
.back-to-top-leave-to {
  opacity: 0;
  transform: translateY(20px) scale(0.8);
}

.back-to-top-enter-to,
.back-to-top-leave-from {
  opacity: 1;
  transform: translateY(0) scale(1);
}

/* 自定义样式变量 */
.bg-primary {
  background-color: #FF6B8B;
}

.hover\:bg-accent:hover {
  background-color: #FF4D6D;
}

/* 响应式设计 */
@media (max-width: 768px) {
  button {
    bottom: 1rem;
    right: 1rem;
    width: 2.5rem;
    height: 2.5rem;
  }
  
  button i {
    font-size: 0.875rem;
  }
}
</style>
